home *** CD-ROM | disk | FTP | other *** search
- /*
- FileUtils.c
-
- Created 05 May 1992 Extracted from Dragon.c
- Modified
-
- */
-
- #include "FileUtils.h"
-
- OSType **FREFTypes (short *numTypesReturned)
- {
- short numFREFs, frefNum, numTypes = 0;
- OSType type, **typesHndl, **fref;
-
- /* NOTE: This function will only work on the most recently opened resource file
- — we have to call Count1Resources and Get1IndResource because
- the System file (at least in System 7) has 'FREF' resources of its own,
- but we want to ignore them — CountResources and GetIndResource
- would screw everything up! */
-
- *numTypesReturned = 0;
- numFREFs = Count1Resources ('FREF');
- if (numFREFs != 0) {
- typesHndl = (OSType **) NewHandle (numFREFs * sizeof (OSType));
- if (typesHndl != NULL) {
- for (numTypes = 0, frefNum = 1; frefNum <= numFREFs; frefNum++) {
- fref = (OSType **) Get1IndResource ('FREF', frefNum);
- if (fref != NULL) {
- type = **fref;
- DisposHandle ((Handle) fref);
- if (type != 'APPL') // Don't count the 'APPL' type — every application
- (*typesHndl) [numTypes++] = type; // has an 'FREF' for this
- }
- }
- if (numTypes < numFREFs)
- SetHandleSize ((Handle) typesHndl, numTypes * sizeof (OSType));
- }
- }
- *numTypesReturned = numTypes;
- return typesHndl;
- }
-
- Boolean OpenableType (OSType fileType, short numOKTypes, OSType **OKTypesHndl)
- {
- short i;
- OSType t, *tp = *OKTypesHndl;
-
- for (i = numOKTypes; i > 0; i--, tp++) {
- t = *tp;
- if (fileType == 'fold' || fileType == 'disk') {
- if (t == fileType)
- return TRUE;
- } else if (t == '****' || t == fileType)
- return TRUE;
- }
- return FALSE; // If we got here, there's no match
- }
-
- OSType FSpType (FSSpec *fss, OSErr *err)
- {
- CInfoPBRec pb;
- long dirID;
-
- // If the parent ID of an FSSpec object (file, folder, volume) == 1, it's a volume —
- // I don't know if this is ALWAYS the case, but it seems to work for me. It has
- // NOT, however, been tested in a networked environment — what would the
- // parID be for an AppleShare volume, for example? The rules might be totally
- // different. On the other hand, Apple might have said in some documentation
- // (that I haven't read) that a directory ID of 1 means exactly this. Wish I knew
- // for sure…
- dirID = fss->parID;
- if (dirID == 1) {
- *err = noErr;
- return 'disk';
- }
- pb.hFileInfo.ioNamePtr = fss->name;
- pb.hFileInfo.ioVRefNum = fss->vRefNum;
- pb.hFileInfo.ioFDirIndex = 0;
- pb.hFileInfo.ioDirID = dirID;
- *err = PBGetCatInfoSync (&pb);
-
- // Bit 4 (where 0 is the least significant, i.e. rightmost, bit) of the ioFlAttrib field will
- // be set after the call to PBHGetInfo if this is in fact a folder and not a file
- if (pb.hFileInfo.ioFlAttrib & 16)
- return 'fold';
- else
- return pb.hFileInfo.ioFlFndrInfo.fdType;
- }
-
- Boolean FSpOpenableType (FSSpec *fss, short numOKTypes, OSType **OKTypesHndl)
- {
- OSType fileType;
- OSErr err;
-
- fileType = FSpType (fss, &err);
- if (err)
- return FALSE;
- else if (OKTypesHndl != NULL)
- return OpenableType (fileType, numOKTypes, OKTypesHndl);
- }
-
-